﻿using System;
using System.IO;
namespace SangTaoT1
{
    /*------------------------
     *       Xep viec
     * ----------------------*/
    class XepViec
    {
        const int mn = 280;
        const string fn = "Viec.inp";
        const string gn = "Viec.out";
        static public Viec[] v; // cac viec
        static public int n = 0; // so luong viec
        static public int tong = 0;
        static public int[] h;
        static public int k = 0;
        static void Main()
        {
            Doc();
            QSort(0, n - 1);
            Xep();
            Ghi();
            Test();
            Console.ReadLine();
        } // Main    
        /**
         * Sap xep cac viec
         * */
        static void Xep()
        {
            // Tim Tmax
            int tmax = 0;
            for (int i = 0; i < n; ++i)
                if (v[i].t > tmax) tmax = v[i].t;
            int tt = tmax + n + 1;
            h = new int[tt];
            // Khoi tri cho h
            for (int i = 0; i < tt; ++i) h[i] = 0;
            tong = 0;
            for (int i = 0; i < n; ++i)
            {
                int td = v[i].t;
                while (h[td] > 0) --td;
                if (td == 0)
                    h[++tmax] = v[i].id; // dat viec khong thuong
                else { h[td] = v[i].id; tong += v[i].thuong; }
            }
            // Dich cac viec len phia truoc
            k = 0;
            for (k = 1; k <= tmax; ++k)
                if (h[k] == 0) break;
            for (int i = k + 1; i <= tmax; ++i)
                if (h[i] > 0)
                    h[k++] = h[i];
        }
        static void Ghi() // Ghi file
        {
            StreamWriter g = File.CreateText(gn);
            for (int i = 1; i < k; ++i) g.WriteLine(h[i]);
            g.WriteLine(tong); g.Close();
        }
        // Sap cac viec giam theo tien thuong
        static void QSort(int d, int c)
        {
            int i = d;
            int j = c;
            int m = v[(d + c) / 2].thuong;
            Viec t = new Viec(0, 0, 0);
            while (i <= j)
            {
                while (v[i].thuong > m) ++i;
                while (m > v[j].thuong) --j;
                if (i <= j)
                {
                    t = v[i]; v[i] = v[j]; v[j] = t;
                    ++i; --j;
                }
            }
            if (d < j) QSort(d, j);
            if (i < c) QSort(i, c);
        }
        // Doc lai file gn de kiem tra ket qua
        static void Test()
        {
            Console.WriteLine("\n Kiem tra lai ket qua");
            Console.WriteLine("\nInput: \n");
            Console.WriteLine(File.ReadAllText(fn));
            Console.WriteLine("\nOutput: \n");
            Console.WriteLine(File.ReadAllText(gn));
        }
        static void Doc()
        {
            int[] a = Array.ConvertAll(
                       (File.ReadAllText(fn)).Split(
                       new char[] { '\n', '\t', '\0', '\r' },
                       StringSplitOptions.RemoveEmptyEntries),
                       new Converter<string, int>(int.Parse));
            n = a[0];
            v = new Viec[n];
            Console.WriteLine(" n = " + n);
            int k = 1;
            for (int i = 0; i < n; ++i)
                v[i] = new Viec(a[k++], a[k++], i + 1);
        }
        public struct Viec
        {
            public int t;
            public int thuong;
            public int id;
            public Viec(int th, int thg, int nn)
            { t = th; thuong = thg; id = nn; }
        }
    } // class
} // space
